package com.zhu.storesmanager.views;

import com.zhu.storesmanager.R;
import com.zhu.storesmanager.SvStoreCompeteActivity;
import com.zhu.storesmanager.SvVisitTaskActivity;
import com.zhu.storesmanager.models.CompeteProductItem;
import com.zhu.storesmanager.models.VisitTaskInfo;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Check弹窗界面
 */
public class PopCompeteInfoView extends RelativeLayout{

    private FrameLayout mContentView = null; //父View

    public PopCompeteInfoView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public PopCompeteInfoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public PopCompeteInfoView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    /**
     * @param layout 需要添加文本弹窗的父界面
     * 
     */
    public void init(FrameLayout layout){
        mContentView = layout;
        setOnClickListener(mListener);
        ImageView close = (ImageView) findViewById(R.id.checkpop_close);
        close.setOnClickListener(mListener);
    }

    /**
     * 显示弹窗界面
     * @param info 竞品信息项
     * @param type 类型 2==新品功能  3==竞品活动
     * @param titleID 标题ID
     */
    public void showPopView(CompeteProductItem info, int type, int titleID){
        TextView titleView = (TextView)findViewById(R.id.checkpop_title);
        titleView.setText(titleID);
        setTextViewData(R.id.competeinfo_brand, R.string.sales_brands, info.mBrandName);
        setTextViewData(R.id.competeinfo_model, R.string.model, info.mModelName);
        setTextViewData(R.id.competeinfo_price, R.string.price, info.mPrice);
        if(type == SvStoreCompeteActivity.TYPE_COMPETE_CONTENT_FUCTION){
            findViewById(R.id.competeinfo_sellcount).setVisibility(View.GONE);
            setTextViewData(R.id.competeinfo_data, R.string.function, info.mDataNames);
            setTextViewData(R.id.competeinfo_other, R.string.otherfunction, info.mOther);
        }else{
            findViewById(R.id.competeinfo_sellcount).setVisibility(View.VISIBLE);
            setTextViewData(R.id.competeinfo_sellcount, R.string.salescount, info.mSellcount);
            setTextViewData(R.id.competeinfo_data, R.string.compete_activity, info.mDataNames);
            setTextViewData(R.id.competeinfo_other, R.string.compete_otheractivity, info.mOther);
        }
        ImageView image = (ImageView)findViewById(R.id.competeinfo_photo);
        if(info.mPhoto != null){
            image.setImageBitmap(info.mPhoto);
        }else{
            image.setImageBitmap(null);
        }
        if(mContentView.indexOfChild(this) == -1){
        	mContentView.addView(this);
        }
    }

    /**
     * 设置文本数据
     * @param parentId 父界面ID
     * @param titleId 标题ID
     * @param content 内容
     */
    private void setTextViewData(int parentId, int titleId, String content){
        View view = findViewById(parentId);
        TextView titleView = (TextView)view.findViewById(R.id.title);
        titleView.setText(titleId);
        TextView contentView = (TextView)view.findViewById(R.id.content);
        contentView.setText(content);
    }
    
    /**
     * 关闭弹窗界面
     */
    public void closePopView(){
        if(mContentView.indexOfChild(this) != -1){
            mContentView.removeView(this);
        }
    }

    /**
     * 处理返回键
     * @return true 表示截获返回键处理弹窗消息
     */
    public boolean doBack(){
        if(mContentView.indexOfChild(this) != -1){
            mContentView.removeView(this);
            return true;
        }
        return false;
    }

    /**
     * 点击时间监听
     */
    private OnClickListener mListener = new OnClickListener(){
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(v != null){
                if(v.getId() == R.id.checkpop_close){ //点击关闭
                    closePopView();
                }
            }
        }
    };   
}
